package com.googlecode.mcwalle.response;

import java.util.Iterator;

/**
 * @author Li Weibo (weibo.leo@gmail.com) //I believe spring-brother
 * @since 2011-10-28 下午07:05:21
 */
public class StatsCachedumpResponse implements Iterable<ItemInfo>{
	
	private Iterator<String> textLineIterator;
	
	public StatsCachedumpResponse(TextLineResponse res) {
		this.textLineIterator = res.iterator();
	}

	@Override
	public Iterator<ItemInfo> iterator() {
		return new Iterator<ItemInfo>() {

			@Override
			public boolean hasNext() {
				return textLineIterator.hasNext();
			}

			@Override
			public ItemInfo next() {
				
				String line = textLineIterator.next();
				
				//line的格式应该为
				//ITEM cache_item_key [4 b; 1319855765 s]
				
				String[] fields = line.split(" ");
				String itemKey = fields[1];
				
				//由于后面包含多个空格，所以只能用substring来提取信息
				String itemInfo = line.substring(fields[0].length() + 1 + fields[1].length());
				
				String[] ss = itemInfo.trim().substring(1, itemInfo.length() - 1).split(";");
				int bytes = Integer.parseInt(ss[0].trim().split(" ")[0]);
				int expireTime = Integer.parseInt(ss[1].trim().split(" ")[0]);
				
				ItemInfo info = new ItemInfo();
				info.setKey(itemKey);
				info.setBytes(bytes);
				info.setExpire(expireTime);
				return info;
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}
	
}
